/**
 * NavBar.m by Marc Bender
 * based on Code by Giraldo Rosales.
 * Visit www.liquidgear.net for documentation and updates.
 *
 * Copyright (c) 2009 Nitrogen Design, Inc. All rights reserved.
 * 
 * Permission is hereby granted, free of charge, to any person
 * obtaining a copy of this software and associated documentation
 * files (the "Software"), to deal in the Software without
 * restriction, including without limitation the rights to use,
 * copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following
 * conditions:
 * 
 * The above copyright notice and this permission notice shall be
 * included in all copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
 * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
 * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
 * OTHER DEALINGS IN THE SOFTWARE.
 **/
#if LG_USE_NAVBAR

#import "LiquidGearAppDelegate.h"
#import "NavBar.h"
#import "WebController.h"
#import "Utils.h"

@implementation NavBar

@synthesize isVisible, navBar, navButtons, navItems, viewController;

- (id)initWithController:(WebController *)controller {
	self.viewController = controller;
	
	CGRect winRect	= [[UIScreen mainScreen] applicationFrame];
	int viewHeight	= 48;
	
	if(LG_NAV_PROMPT) {
		viewHeight	= viewHeight + 20;
	}
	
	navBar = [[UINavigationBar alloc] initWithFrame:CGRectMake(0, 0, winRect.size.width, viewHeight)];
	navBar.hidden		= TRUE;
	navBar.tag			= 20;
	navBar.delegate	= self;
	navButtons			= [[NSMutableArray alloc] initWithCapacity:0];
	navItems				= [[NSMutableArray alloc] initWithCapacity:0];
	return self;
}

- (void)addNavItem:(NSString *)title description:(NSString *)desc leftTitle:(NSString *)leftTitle leftCall:(NSString *)leftCall rightTitle:(NSString *)rightTitle rightCall:(NSString *)rightCall hideBack:(NSString *)hideBack {
	UINavigationItem *item	= [[UINavigationItem alloc] initWithTitle:title];
	
	//Hides the back button on defined Pages / subpages could get one, mainpages (reacheable with the tabs) not
	item.hidesBackButton = [hideBack boolValue];
	
	int tagButton	= [navButtons count];
	
	//Left Button
	if (leftTitle != @"") {
		//Add JS call to array for left button
		[navButtons addObject:leftCall];
		
		if ([leftTitle isEqualToString:@"Refresh"]) {
			UIBarButtonItem *btnLeft = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemRefresh target:self action:@selector(doJSAction:)];  
			
			btnLeft.tag = tagButton;	
			item.leftBarButtonItem = btnLeft;
		}
		else if ([leftTitle isEqualToString:@"Search"]) {
			UIBarButtonItem *btnLeft = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemSearch target:self action:@selector(doJSAction:)];  
			
			btnLeft.tag = tagButton;	
			item.leftBarButtonItem = btnLeft;
		}	else {
			UIBarButtonItem *btnLeft = [[UIBarButtonItem alloc] initWithTitle:leftTitle style:UIBarButtonItemStylePlain target:self action:@selector(doJSAction:) ];
			
			btnLeft.tag = tagButton;
			item.leftBarButtonItem = btnLeft;
		}
		
		tagButton++;
	}
	
	
	//Right Button
	if(rightTitle !=@"") {
		//Add JS call to array for left button
		[navButtons addObject:rightCall];
		
		if([rightTitle isEqualToString:@"Refresh"]) {
			UIBarButtonItem *btnRight = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemRefresh target:self action:@selector(doJSAction:)];  
			
			btnRight.tag = tagButton;	
			item.rightBarButtonItem = btnRight;
		}
		else if([rightTitle isEqualToString:@"Search"]) {
			UIBarButtonItem *btnRight = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemSearch target:self action:@selector(doJSAction:)];  
			
			btnRight.tag = tagButton;	
			item.rightBarButtonItem = btnRight;
		}	else {
			UIBarButtonItem *btnRight = [[UIBarButtonItem alloc] initWithTitle:rightTitle style:UIBarButtonItemStylePlain target:self action:@selector(doJSAction:)];
			
			btnRight.tag = tagButton;
			item.rightBarButtonItem = btnRight;
		}
	}
	
	if(LG_NAV_PROMPT) {
		//Only if desc is not empty
		if (desc != @"") {
			item.prompt	= desc;
		}
	}
	
	if([navBar.items count] > 0) {
		[navBar pushNavigationItem:item animated:YES];
	} else {
		[navBar pushNavigationItem:item animated:NO];
	}
}

- (void)navigationBar:(UINavigationBar *)navigationBar didPopItem:(UINavigationItem *)item {
}

- (BOOL)navigationBar:(UINavigationBar *)navigationBar shouldPopItem:(UINavigationItem *)item {
	[[[Utils alloc] autorelease] jsCall:viewController.webView type:@"" data:nil directCall:@"lg.navigation.callback({back:true})"];
	return TRUE;
}

- (void)doJSAction:(id)btn {
	int tagButton			= [btn tag];
	NSString *jsCall	= [navButtons objectAtIndex:tagButton];
	
	//Get the assigned JS call
	[[[Utils alloc] autorelease] jsCall:viewController.webView type:@"" data:nil directCall:jsCall];
}

- (void)showBar:(int)barStyle tintColor:(UIColor*)tintColor translucent:(bool)translucent {
	LiquidGearAppDelegate *LGdelegate = (LiquidGearAppDelegate*)[[UIApplication sharedApplication] delegate];
	
	navBar.hidden					= FALSE;
	navBar.barStyle				= barStyle;
	navBar.tintColor			= tintColor;
	navBar.translucent		= translucent;
	
	CGRect winRect	= viewController.webView.frame;
	CGRect navRect	= navBar.frame;
	
	winRect.size.height	-= 48;
	winRect.origin.y		+= 48;
	
	navRect.origin.y		= 0;
	
	if(LG_NAV_PROMPT) {
		winRect.size.height	-= 20;
		winRect.origin.y		+= 20;
	}
	
	navBar.frame									= navRect;
	viewController.webView.frame	= winRect;
}

- (void)hideBar {
	LiquidGearAppDelegate *LGdelegate = (LiquidGearAppDelegate*)[[UIApplication sharedApplication] delegate];
	
	navBar.hidden	= TRUE;
		
	//Resize Frame
	CGRect winRect			= LGdelegate.currentRect;
	winRect.size.height	+= 48;
	winRect.origin.y		= 0;
	
	LGdelegate.currentRect	= winRect;
	
	//[LGdelegate.window bringSubviewToFront:viewController.webView];
	viewController.webView.frame = winRect;
}

- (void)dealloc {
	[navBar release];
	[viewController release];
	[super dealloc];
}

@end
#endif
